import com.foreveross.activiti.service.ActivitiService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.identity.Group;
import org.activiti.engine.task.Task;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static junit.framework.TestCase.assertEquals;

/**
 * author:wzq
 * project:01_hello
 * date:16-5-11
 */
public class _07_MyApiTest_02 {

    private static ProcessEngine processEngine;

    private static String user1 = "kermit";
    private static String user2 = "bob";
    private static String gId = "management";
    private static String gName = "management";
    private static ActivitiService activitiService;

    @BeforeClass
    public static void init() {
        processEngine = ProcessEngineConfiguration
                .createStandaloneInMemProcessEngineConfiguration()
                .buildProcessEngine();

        prepareMemship(processEngine.getIdentityService());

        // deploy process definition
        processEngine.getRepositoryService()
                .createDeployment()
                .addClasspathResource("_07_leave_v1.bpmn20.xml")
                .deploy();

        activitiService = new ActivitiService(processEngine);
    }

    private static void prepareMemship(IdentityService identityService) {
        // create user
        identityService.saveUser(identityService.newUser(user1));
        identityService.saveUser(identityService.newUser(user2));

        // create group
        Group newGroup = identityService.newGroup(gId);
        newGroup.setName(gName);
        identityService.saveGroup(newGroup);

        //Creates a group membership
        identityService.createMembership(user2, gId);
    }


    @Test
    public void testMembership() {
        // query all process definition
        List<Map<String, String>> definitionList = activitiService.getProcessDefinitionListV2();
        assertEquals(definitionList.size(), 1);
        System.out.println("step 1 done");

        // process definition detail
        Map<String, String> definitionInfo = definitionList.get(0);
        System.out.println(definitionInfo);

        // start process instance
        String processDefinitionId = definitionInfo.get("id");
        Map<String, String> initParams = new HashMap<String, String>();
        initParams.put("days", "12");
        String processInstanceId = activitiService.startProcessInstance(user1, processDefinitionId, initParams);
        System.out.println("step 2 done processInstanceId=" + processInstanceId);

        // query user tasks of user2
        List<Task> userAssignTask = activitiService.taskAssignee(user2);
        assertEquals(userAssignTask.size(), 0);
        List<Task> taskCandidateGroup = activitiService.taskCandidateGroup(gId);
        assertEquals(taskCandidateGroup.size(), 1);
        // List<Task> userTaskCandidate = activitiService.taskCadidateUser(user2);
        // assertEquals(userTaskCandidate.size(), 1);
        System.out.println("step 3 done");

        // user2 claim task
        String taskId = taskCandidateGroup.get(0).getId();
        activitiService.claimTask(user2, taskId);
        List<Task> u2Tasks = activitiService.taskAssignee(user2);
        assertEquals(u2Tasks.size(), 1);
        System.out.println("step 4 done");

        // check the task
        Task u2Task = u2Tasks.get(0);
        System.out.println(u2Task.getDescription());
        Map<String, Object> processVariables = activitiService.getProcessVariables(u2Task.getId());
        System.out.println(processVariables);
        System.out.println("step 5 done");

        // user 2 complete task  approve = false
        Map<String, String> taskParams = new HashMap<String, String>();
        taskParams.put("approve", "false");
        System.out.println(u2Task);
        activitiService.completeTask(u2Task.getId(), taskParams);
        System.out.println("step 6 done");

        // now check task of user1
        List<Task> u1Tasks = activitiService.taskAssignee(user1);
        assertEquals(u1Tasks.size(), 1);
        Task u1Task = u1Tasks.get(0);
        processVariables = activitiService.getProcessVariables(u1Task.getId());
        System.out.println(processVariables);
        System.out.println("step 7 done");

        // user1 complete task
        Map<String, String> adjustParams = new HashMap<String, String>();
        adjustParams.put("days", "3");
        activitiService.completeTask(u1Task.getId(), adjustParams);
        System.out.println("step 8 done");

        // user2 complete task
        u2Tasks = activitiService.taskCadidateUser(user2);
        assertEquals(u2Tasks.size(), 1);
        u2Task = u2Tasks.get(0);
        activitiService.claimTask(user2, u2Task.getId());
        taskParams = new HashMap<String, String>();
        processVariables = activitiService.getProcessVariables(u2Task.getId());
        System.out.println(processVariables);
        taskParams.put("approve", "true");
        activitiService.completeTask(u2Task.getId(), taskParams);
        System.out.println("step 9 done");
    }
}
